##
## 
## Divergent Effects of Institutions on Different Types of Coups 
## 
## Nam and Jun's Project :)
##
## September 2020

#install.packages(c("dplyr", "magrittr","cowplot"))
library(foreign)
library(MASS)
#install.packages("Zelig")
library(Zelig)
#install.packages('zeligverse')
library(zeligverse)
options(scipen=999) ## no scientific notation (e--)
# Load packages
library(magrittr)
library(dplyr)

library(cowplot) # multi plots

## downloading the factor-level-change commands
source("https://raw.githubusercontent.com/janhove/janhove.github.io/master/RCode/sortLvls.R")


#### 
#### Making figure 
#### 
library(ggplot2)

credplot.gg <- function(d){
  # d is a data frame with 4 columns
  # d$x gives variable names
  # d$y gives center point
  # d$ylo gives lower limits
  # d$yhi gives upper limits
  require(ggplot2)
  p <- ggplot(d, aes(x=x, y=y, ymin=ylo, ymax=yhi))+
    geom_pointrange(colour= "blue")+
    geom_hline(yintercept = 0, linetype=2)+ 
    scale_y_continuous(limits=c(-0.05, 0.05) , breaks=seq(-0.05, 0.05, 0.02 )) +
    geom_hline(yintercept=t[1], colour= "deeppink3" , linetype="twodash" ) + 
    geom_hline(yintercept=-t[1], colour= "deeppink3" , linetype="twodash" ) + 
    #geom_hline(yintercept=t7[1] , colour="green", linetype="twodash") + 
    #geom_hline(yintercept=-t7[1] , colour="green", linetype="twodash") + 
    #geom_hline(yintercept=t8[1] , colour="blue", linetype="twodash") + 
    #geom_hline(yintercept=-t8[1] , colour="blue", linetype="twodash") + 
    theme_bw() +
    coord_flip()+  
    xlab(" ") +
    ylab("Averaged Marginal Effects") + 
    ggtitle("Reshuffling Coup DV") +
    theme(plot.title = element_text( size=14, face="bold"),
          axis.title.x = element_text( size=14 ),
          axis.title.y = element_text(  size=14 )) +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          panel.background = element_blank(), axis.line = element_line(colour = "black"))
  
  return(p)
}




#####
#### Make the dataframe 
#### 
x <- c("Party", "Legislature", "Party and Legislature") 

y <- c(   -.0234 ,    -.0159,  -.0199  ) 
ylo <- c(  -.0343,  -.0255,  -.0298 )#CI around predicted/expected value of exit 
yhi <- c(    -.0124,  -.0063,  -.0099 ) 

t <- rep( -0.0204,3)  #meaningful threshold == average proportion of coups in the sample 
#t7 <- rep(- .01379  , 3 ) 
#t8 <-rep(-.01576 , 3 ) 
tgc <- data.frame(x, y, ylo, yhi, t) 

tgc$x <- as.factor(tgc$x)
## checking the order of factors 
levels(tgc$x)  # [1] "Legislature_M2" "Legislature_M3" "Party_M1"       "Party_M3"  

## Manuary changing the order of factor level 

tgc$x <- sortLvls.fnc(tgc$x, c(3,1,2))
levels(tgc$x)
 

## plot1  
p1 <- credplot.gg(tgc)




#### Regime-Change Coup

credplot.gg2 <- function(d){
  # d is a data frame with 4 columns
  # d$x gives variable names
  # d$y gives center point
  # d$ylo gives lower limits
  # d$yhi gives upper limits
  require(ggplot2)
  p <- ggplot(d, aes(x=x, y=y, ymin=ylo, ymax=yhi))+
    geom_pointrange(colour= "blue")+
    geom_hline(yintercept = 0, linetype=2)+
    scale_y_continuous(limits=c(-0.06, 0.06) , breaks=seq(-0.06, 0.06, 0.02 )) +
    geom_hline(yintercept=t[1] , colour="deeppink3", linetype="twodash") + 
    geom_hline(yintercept=-t[1] , colour="deeppink3", linetype="twodash") + 
   # geom_hline(yintercept=t7[1] , colour="green", linetype="twodash") + 
   #geom_hline(yintercept=-t7[1] , colour="green", linetype="twodash") + 
    #geom_hline(yintercept=t8[1] , colour="blue", linetype="twodash") + 
    #geom_hline(yintercept=-t8[1] , colour="blue", linetype="twodash") + 
    theme_bw() +  
    coord_flip()+
    ylab("Averaged Marginal Effects") + 
    xlab("") + 
    ggtitle("Regime-Change Coup DV") +
    theme(plot.title = element_text( size=14, face="bold"),
          axis.title.x = element_text( size=14 ),
          axis.title.y = element_text(  size=12 )) +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          panel.background = element_blank(), axis.line = element_line(colour = "black"))
  
  return(p)
}




#####
#### Make the dataframe 
#### 
x <- c("Party","Legislature", "Party and Legislature" )  

y<- c(   -.0083,    .0019 , -.0027    )
ylo <- c(  -.0222 ,  -.0143   , -.0159  )#CI around predicted/expected value of exit 
yhi <- c(       .0056, .0181,   .0105) 

t <- rep(-.03368 , 3 ) # meaningful threshold  
#t7 <- rep(-.01673  , 3 ) 
#t8 <-rep(- .01912 , 3 ) 
tgc <- data.frame(x, y, ylo, yhi) 


tgc$x <- as.factor(tgc$x)
## checking the order of factors 
levels(tgc$x)  # [1] "Legislature_M2" "Legislature_M3" "Party_M1"       "Party_M3"  

## Manuary changing the order of factor level 

tgc$x <- sortLvls.fnc(tgc$x, c(  3,1,2 ))
levels(tgc$x)




p2 <- credplot.gg2(tgc)




## Combine multi plots   
 
plot_grid(p2, p1 , ncol = 2)





## save 
savePlot(filename="effect", type = "pdf")

 





##
## end: 
##

